package com.cxm.weilai.util;

import java.text.FieldPosition;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * UUID工具包
 * @author admin
 */
public class UUIDGenerator {

    private static Format dateFormat = new SimpleDateFormat("yyMMddHHmmssS");

    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

    /**
     * 获取UUID
     * @return String
     */
    public static String getUUID() {
        String string = UUID.randomUUID().toString();

        StringBuffer sb = new StringBuffer();
        Calendar rightNow = Calendar.getInstance();
        sb.append(string.substring(0, 8));
        sb.append(string.substring(9, 13));
        sb.append(string.substring(14, 18));
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        sb.append(string.substring(19, 23));
        sb.append(string.substring(24));
        return sb.toString();
    }

    /**
     * 获取UUID
     * @return String[]
     */
    public static String[] getUUID(int number) {
        if (number < 1) { return null; }
        String[] ss = new String[number];
        for (int i = 0; i < number; ++i) {
            ss[i] = getUUID();
        }
        return ss;
    }

}
